package com.lzx.shop.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzx.shop.dataobject.AddressDO;
import com.lzx.shop.model.Result;
import com.lzx.shop.service.AddressService;
import com.lzx.shop.utils.ResultUtil;
import com.lzx.shop.utils.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName AddressApi.java
 * @Description TODO
 * @createTime 2021年04月06日 21:45:00
 */
@Api(tags = "AddressApi",description = "地址管理")
@RestController
@RequestMapping("/address")
public class AddressApi {

    @Autowired
    private AddressService addressService;

    @ApiOperation("插入或更新地址")
    @PostMapping("/saveOrUpdate")
    public Result  saveOrUpdate(@RequestBody AddressDO addressDO){

        if (addressDO==null){
            ResultUtil.paramError();
        }
        if (StringUtils.isBlank(addressDO.getId())){
            addressDO.setId(UUIDUtil.getUUID());
        }
        boolean result =  addressService.saveOrUpdate(addressDO);
        return  ResultUtil.success(result);
    }

    @ApiOperation("根据用户id查询")
    @GetMapping("/getByUserId/{id}")
    public Result getByUserId(@PathVariable("id")String id){

        if (StringUtils.isBlank(id)){
            return ResultUtil.paramError();
        }
        List<AddressDO> addressDOList = addressService.list(new QueryWrapper<AddressDO>().lambda().eq(AddressDO::getUserId,id));
        return ResultUtil.success(addressDOList);
    }

    @ApiOperation("根据id删除")
    @GetMapping("/del/{id}")
    public Result del(@PathVariable("id")String id){
        if (StringUtils.isBlank(id)){
            return ResultUtil.paramError();
        }

        boolean remove = addressService.lambdaUpdate().eq(AddressDO::getId,id).remove();

        return ResultUtil.success(remove);
    }

    @ApiOperation("查询全部")
    @GetMapping("/getAll")
    public Result getAll(){
        List<AddressDO> list = addressService.lambdaQuery().list();

        return ResultUtil.success(list);
    }
}
